package main.java.DMDE.model;

import java.util.*;

/**
 * Class Method
 */
public class Method {

	private List<Parameter> parameters = null;
	private Type returnType;
	private List<Modifier> modifiers = null;
	private String name;
	private Visibility visibility;

	//
	// Constructors
	//
	
	public Method() {
		parameters = new ArrayList<Parameter>();
		modifiers = new ArrayList<Modifier>();		
	}
	
	public Method(String name) {
		this.setName(name);
		parameters = new ArrayList<Parameter>();
		modifiers = new ArrayList<Modifier>();
	}

	/**
	 * @return the parameters
	 */
	public List<Parameter> getParameters() {
		return parameters;
	}

	/**
	 * Returns true if already exists a modifier with the same name
	 */
	private boolean containsModifier(Modifier modifier) {
		return modifiers.contains(modifier);
	}

	/**
	 * @param parameters
	 *            the parameters to set
	 */
	public void setParameters(List<Parameter> parameters) {
		this.parameters = parameters;
	}

	/**
	 * @return the returnType
	 */
	public Type getReturnType() {
		return returnType;
	}

	/**
	 * @param returnType
	 *            the returnType to set
	 */
	public void setReturnType(Type returnType) {
		this.returnType = returnType;
	}

	/**
	 * @return the modifiers
	 */
	public List<Modifier> getModifiers() {
		return modifiers;
	}

	/**
	 * @param modifiers
	 *            the modifiers to set
	 */
	public void setModifiers(List<Modifier> modifiers) {
		this.modifiers = modifiers;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	public void addParameter(Parameter parameter) {
		if (!containsParameter(parameter))
			parameters.add(parameter);
	}

	private boolean containsParameter(Parameter parameter) {
//		return parameters.contains(parameter);
	    for(Parameter param: parameters){
		if(param.getName().equals(parameter.getName())){
		    return true;
	    	}
	    }
	    return false;
	}

	public void addModifier(Modifier modifier) {
		if (!this.containsModifier(modifier)) {
			this.modifiers.add(modifier);
		}
	}

	public int countParameters() {
		return parameters.size();
	}

	public Parameter getParameter(String nombre) {
		for (Parameter param : parameters) {
			if (param.is(nombre)) {
				return param;
			}
		}
		return null;
	}

	public boolean isNamed(String methodName) {
		return methodName != null ? this.name.equals(methodName) : false;
	}

	public Visibility getVisibility() {
		return visibility;
	}

	public void setVisibility(Visibility visibility) {
		this.visibility = visibility;
	}

	public void addParameters(List<Parameter> parameters) {
	    for(Parameter param: parameters)
		addParameter(param);
	};

	@Override
	public String toString(){
        	String completeName = this.getName() + "( ";
        	List<Parameter> parameters = this.getParameters();
        	for (Parameter param : parameters)
        	    completeName += param.getName() + " : " + param.getType().getName() + " ,";
        	completeName += ")";
        	if (!parameters.isEmpty()){
        	    completeName = completeName.replaceAll(",\\)", "\\)");
        	}
        	if(this.getReturnType() != null)
        	    completeName += " : " + this.getReturnType().getName();
        	return completeName;
	}
}
